#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

#include <sw/redis++/redis++.h>

#include "util.hpp"

using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::unordered_map;

namespace redis = sw::redis;

void test_hset_and_hget(redis::Redis& redis){
    cout << "test hset and hget" << endl;
    redis.flushall();

    // 添加单个
    redis.hset("key1", "zhangsan", "80");
    redis.hset("key1", {"lisi", "90"});
    
    // 添加多个
    // 初始化列表
    redis.hset("key1", {
        std::make_pair("wangwu", "88"),
        std::make_pair("zhaoliu", "99")
    });
    // 容器
    unordered_map<string, string> in = {
        {"Amy", "66"},
        {"Daming", "77"}
    };
    redis.hset("key1", in.begin(), in.end());

    // 获取元素
    auto res = redis.hget("key1", "Sam");
    if(res){
        cout << "Sam score: " << res.value() << endl;
    }
    else{
        cout << "Sam not exist" << endl;
    }

    vector<string> values;
    auto it = std::back_inserter(values);
    redis.hgetall("key1", it);
    printContainer(values);
}

void test_hexists_and_hdel(redis::Redis& redis){
    cout << "test hexists and hdel" << endl;
    redis.flushall();

    redis.hset("key1", "zhangsan", "80");
    // 判断是否存在
    auto res = redis.hexists("key1", "zhangsan");
    cout << "zhangsan exists: " << res << endl;

    // 删除，可删除一个或多个
    // 返回值是成功删除的元素个数
    auto num = redis.hdel("key1", {"zhangsan", "lisi", "wangwu"});
    cout << "hdel num: " << num << endl;

    // 再次判断是否存在
    res = redis.hexists("key1", "zhangsan");
    cout << "zhangsan exists: " << res << endl;
}

void test_hkeys_and_hvals(redis::Redis& redis){
    cout << "test hkeys and hvals" << endl;
    redis.flushall();

    redis.hset("key1", {
        std::make_pair("wangwu", "88"),
        std::make_pair("zhaoliu", "99"),
        std::make_pair("Amy", "66"),
        std::make_pair("Daming", "77")
    });

    // 获取所有 key
    vector<string> keys;
    auto it = std::back_inserter(keys);
    redis.hkeys("key1", it);
    printContainer(keys);

    // 获取所有 value
    vector<string> values;
    it = std::back_inserter(values);
    redis.hvals("key1", it);
    printContainer(values);
}

void test_hmset_and_hmget(redis::Redis& redis){
    cout << "test hmset and hmget" << endl;
    redis.flushall();

    // 添加多个
    // 初始化列表
    redis.hmset("key1", {
        std::make_pair("zhangsan", "80"),
        std::make_pair("lisi", "90")
    });
    // 容器
    unordered_map<string, string> in = {
        {"wangwu", "88"},
        {"zhaoliu", "99"}
    };
    redis.hmset("key1", in.begin(), in.end());

    // 获取多个
    vector<string> values;
    auto it = std::back_inserter(values);
    redis.hmget("key1", {"zhangsan", "lisi", "wangwu"}, it);
    printContainer(values);
}

int main(){
    redis::Redis redis("tcp://666666@127.0.0.1:6379");

    // test_hset_and_hget(redis);
    // test_hexists_and_hdel(redis);
    // test_hkeys_and_hvals(redis);
    test_hmset_and_hmget(redis);


    return 0;
}

